﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Configuration;
using  Common.Models;

namespace StockServer
{
    class AsySocket
    {

        Dictionary<string, ClientModel> clients = new Dictionary<string, ClientModel>();
        int backlog=10;
        Socket m_Socket = null;
        AsySocket()
        {
            string port = ConfigurationManager.AppSettings["Port"];
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_Socket.Listen(backlog);
            m_Socket.BeginAccept(new AsyncCallback(AcceptCallBack), null);//异步
        }
         AsySocket(Socket linkObject)
        {
            m_Socket = linkObject;
          
        }



        public void SendMsg(string msg)
        {

        }

        public void BeginListening(string msg)
        {
            while (true)
            {

            }
        }



        private void AcceptCallBack(IAsyncResult ar)
        {
            Socket handler = m_Socket.EndAccept(ar);
            AsySocket NewSocket = new AsySocket(handler);
            //激发事件
            //if (onAccept != null)
            //    onAccept(NewSocket);
            //重新监听
            m_Socket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
        }


    }
}
